package com.wen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.R;
import com.wen.pojo.Stock;
import com.wen.pojo.vo.StockVo;
import com.wen.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/stock")
public class StockController {
    @Autowired
    private StockService service;

    @GetMapping("/list")
    public R list(){
        List<Stock> list = service.list();
        return R.ok().data("list",list);
    }
    @PutMapping("/stUp/{event}/{id}")
    public R stUp(@PathVariable Long event,@PathVariable Long id){
        boolean flag = service.StoUp(event, id);
        if(flag)
        return R.ok().message("启用成功");
        return R.error().message("启用失败");
    }
    @PostMapping("/page/{pageNum}/{pageSize}")
    public R page(@RequestBody StockVo stockVo, @PathVariable Long pageNum, @PathVariable Long pageSize){
        Page<Stock> stockPage = service.pageListStock(stockVo, pageNum, pageSize);
        return R.ok().data("files",stockPage.getRecords()).data("total",stockPage.getTotal())
                .data("current",stockPage.getCurrent()).data("size",stockPage.getSize());
    }

    @PostMapping("/stockAdd")
    public R stockAdd(@RequestBody Stock stock){
        boolean flag = service.save(stock);
        if (flag)
            return R.ok().message("添加成功");
        return R.error().message("添加失败");
    }
    @GetMapping("stockById/{id}")
    public R stockById(@PathVariable Long id){
        Stock stock = service.getById(id);
        return R.ok().data("stock",stock);
    }
    @PutMapping("UpdateStock")
    public R UpdateStock(@RequestBody Stock stock){
        boolean flag = service.updateById(stock);
        if (flag)
            return R.ok().message("修改成功");
        return R.error().message("修改失败");
    }
}
